<?php
// include function files for this application
require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
if (!session_id()) { session_start(); }

   do_html_header("UNY-Con Create text file of events", staysmall, $uny);

    //connect to the DB
   if (!($conn = db_connect()))
     return false;

$gm_id = $_GET['gm_id'];

$filename = 'cogm.csv';
//fopen ($filename, 'w+');
if (is_writable($filename)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename)</span>";
		exit;
		}
	//print "Success, wrote ($someconent entries) to file ($filename)";
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File is not writeable!</span>";
	}

$result = get_event_cogms($gm_id);
$num = pg_numrows($result);
$i=0;
while ($i < $num) {
	$values = recordproc($result, $i);
	extract($values);
	//This checks to see what timeslot the current record is in. If we have moved to a new timeslot from the previous record we clos the previous table and start a new one, otherwise we write another table cell.
	$current_ts = $starting_timeslot;
	if ($working_ts != $current_ts) {
		$working_ts = $current_ts;
		$tstime = print_timeslot($current_ts);
//		echo "$tstime\n";
		$somecontent = "\n\n$tstime";
		write_to_file($somecontent, $filename);
		$quickcontent = "\n$tstime";
		write_to_file($quickcontent, $filename2);
		}
	$current_event_type = $event_type;
	if ($working_event_type != $current_event_type) {
		$working_event_type = $current_event_type;
		$somecontent = "\n\n$current_event_type\n\"Event Title\",\"System\",\"Cost\",\"GM\",\"K/D\",\"Location\",\"Event Code\"";
		write_to_file($somecontent, $filename);
		}
		unset($processed_numts, $processed_equip, $processed_event_id, $processed_maturity, $print_description);
		//this gets rid of mulitple \n characters.
		$print_description = str_replace("\n\n" , "", $description);
		if ($num_of_timeslots != "1") {$processed_event_id = "TS$starting_timeslot"."$event_id*"; }
			else {$processed_event_id = "TS$starting_timeslot"."$event_id"; }
		if ($num_of_timeslots != "1") {$processed_numts = "\n*Note, this event lasts $num_of_timeslots timeslots"; }
		if ($equipment != "") {$processed_equip = "\n\"Attendees should bring: $equipment\""; }
		if ($maturity == "M") {$processed_maturity = "\n\"This event may contain mature themes\""; }
		if ($maturity == "Y") {$processed_maturity = "\n\"This event will contain mature themes\""; }
		$somecontent = "\n\"$event_title\",\"$game_system\",\""."$"."$cost\",\"$name\",\"$familiarity/$difficulty\",\"$assigned_room\",\"$processed_event_id\"";
		write_to_file($somecontent, $filename);
	$i++;
	}

do_html_URL("$filename", "Download $filename");
do_html_footer(news);